package com.test;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;

public class SocketService extends Service{

	final private String TAG = "SocketService";
	private Socket socket;
	final private IBinder mBinder= new SocketBinder();
	public void onCreate(){
		System.out.println("SocketService created");
		
	}
	
	@Override
	public IBinder onBind(Intent arg0) {
		System.out.println("SocketService onBind");
		return mBinder;
	}

	public class SocketBinder extends Binder{
		SocketService getService(){
			return SocketService.this;
		}
		
	}
	
	//method for clients
	public Socket getSocket(){
		try {
        	
	        socket = new Socket();
			InetAddress address = InetAddress.getByName("192.168.5.2");
			int port = 8000;
			Log.v(TAG, "SocketConnect trying to connect to:"+address+":"+port);
			
			socket.connect(new InetSocketAddress(address, port), 0);
			System.out.println("SocketConnect connected");
			
			} catch (IOException e) {
				System.out.println("EXCEPTION in SocketService!");
				e.printStackTrace();
			}
		return socket;
	}
	
	public void closeSocket() throws IOException{
		socket.close();
	}
	
	class SocketHandler extends Handler{
		
		
	}
}
